//
//  CMDBConditionCollectionView.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/12.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let KMovieWidth : CGFloat = (CMDB_Screen_Width - KleftMargin * 4) / 3

class CMDBConditionCollectionView: BaseCollectionView {
    
    var groupWorks : [CMDBWorksBean]?{
        
        didSet{
            reloadData()
        }
    }
    
    override func commitInitOwnProperties() {
        super.commitInitOwnProperties()
        
        backgroundColor = UIColor.whiteColor()
        
        registerClass(CMDBMovieCollectionCell.self, forCellWithReuseIdentifier: "movieWork")
        
        setupRefresh()
    }
   
}

extension CMDBConditionCollectionView{
    
    func setupRefresh(){
        
        let header = MJRefreshGifHeader(refreshingTarget: self,refreshingAction:#selector(CMDBConditionCollectionView.loadNewStatuses))
        
        header.setValue(true, forKeyPath: "stateLabel.hidden")
        
        header.setValue(true, forKeyPath: "lastUpdatedTimeLabel.hidden")
        
        header.setImages([UIImage(named: "1")!],forState:MJRefreshState.Idle)
        
        header.setImages([UIImage(named: "1")!],forState:MJRefreshState.Pulling)
        
        header.setImages(
            [UIImage(named: "8")!,
                UIImage(named: "7")!,
                UIImage(named: "6")!,
                UIImage(named: "5")!,
                UIImage(named: "4")!,
                UIImage(named: "3")!,
                UIImage(named: "2")!,
                UIImage(named: "1")!],forState:MJRefreshState.Refreshing)
        
        mj_header = header
        
        mj_header.beginRefreshing()
        
//        let footer = MJRefreshAutoFooter(refreshingTarget: self, refreshingAction: #selector(CMDBRefreshTableView.loadMoreStatuses))
        
//        mj_footer = footer
        
//        mj_footer.hidden = true
        
        bounces = true
        
    }
    
}

extension CMDBConditionCollectionView{
    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        
        return CGSizeMake(KMovieWidth, 35 + KMovieWidth / 3 * 4)
    }
    
    override internal func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(15, 10, 15, 15)
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        mDelegate?.onItemClickType!("CollectionViewCellClick", data: groupWorks![indexPath.item], indexPath: indexPath)
    }
}


extension CMDBConditionCollectionView {
    
    @objc private func loadNewStatuses() {
        mDelegate?.onHeaderRefresh!()
    }
    @objc private func loadMoreStatuses() {
        mDelegate?.onFooterRefresh!()
        
    }
}


extension CMDBConditionCollectionView
{
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        printLog(groupWorks?.count ?? 0)
        
        return groupWorks?.count ?? 0
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("movieWork", forIndexPath: indexPath) as! CMDBMovieCollectionCell
        
        cell.movieWork = groupWorks![indexPath.item]
        
        return cell
    }
    
}

